package cn.bingoogolapple.bgabanner.demo.ui;

import cn.bingoogolapple.bgabanner.BGABanner;
import cn.bingoogolapple.bgabanner.demo.App;
import cn.bingoogolapple.bgabanner.demo.ResourceTable;
import cn.bingoogolapple.bgabanner.demo.model.BannerModel;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.window.dialog.ToastDialog;
import ohos.media.image.PixelMap;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import java.util.ArrayList;

public class FrescoDemoAbility extends Ability {
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_f_layout);
        BGABanner bgaBanner = (BGABanner) findComponentById(ResourceTable.Id_banner_fresco);
        bgaBanner.setIconNoElement(ResourceTable.Media_yuan_no);
        bgaBanner.setIconNoElement(ResourceTable.Media_yuan_yes);
        bgaBanner.setmPlaceholderDrawableResId(ResourceTable.Media_holder);
        bgaBanner.setDelegate((banner, itemView, model, position) -> {
            new ToastDialog(getContext()).setText("点击了第" + (position + 1) + "页").show();
        });
        ArrayList<Object> list = new ArrayList<>();
        list.add(ResourceTable.Media_bg7_img);
        list.add(ResourceTable.Media_bg8_img);
        list.add(ResourceTable.Media_bg9_img);
        list.add(ResourceTable.Media_bg10_img);
        list.add(ResourceTable.Media_bg_img);
        bgaBanner.setData(ResourceTable.Layout_f_item_layout, list, null);
        App.getInstance().getEngine().fetchItemsWithItemCount(5).enqueue(new Callback<BannerModel>() {
            @Override
            public void onResponse(Call<BannerModel> call, Response<BannerModel> response) {
            }

            @Override
            public void onFailure(Call<BannerModel> call, Throwable t) {
            }
        });

        bgaBanner.setAdapter(new BGABanner.Adapter<Component, Object>() {
            @Override
            public void fillBannerItem(BGABanner banner, Component itemView, Object model, int position) {
                Image image = (Image) itemView.findComponentById(ResourceTable.Id_itemf);
                image.setPixelMap(App.getInstance().getBitmap(model.toString()));
            }
        });
    }
}
